﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Antlr.Runtime.Tree;
using MVCManage.Api.Models;
using MVCManage.BLL;
using MVCManage.Model;
using MVCProject.API;
namespace MVCManage.Api.Controllers
{
    public class UserController : ApiController
    {
        YhBLL yy = new YhBLL();
        ImagesBLL ill = new ImagesBLL();
       
         [Route("api/xg")]
         public Result PutImage([FromBody] Users value) {
           
            int count =ill.Xg(value.userID,value.imgID);
         
             Result result = new Result();
             result.code = count>0 ? 1 : 0;
             result.msg = count>0 ? "修改成功" : "修改失败";
             return result;
         }

        [Route("api/User/Pdermain")]
       
        public ResultPageList<UserDto> userGet()
        {

           
            var list = yy.Query();
            var zhi = from n in list
                      select new UserDto()
                      {
                          UserID = n.userID,
                          UserName = n.userName,
                          Sex = n.sex,
                          Birthday = n.birthday,
                          Phone = n.phone,
                          Path = n.Images.path,
                          Status = n.status,
                          RoleName = n.Roles.roleName,
                          LoginName = n.loginName,
                          LoginPwd = n.loginPwd,
                          RoleID = n.Roles.roleID
                      };
            ResultPageList<UserDto> result = new ResultPageList<UserDto>();
            result.code = 1;
            result.msg = "ok";
            result.data = zhi.ToList();
            
            return result;
        }
        [Route("api/page")]
        public ResultPageList<UserDto> Get(string name, int page, int limit)
        {

            int count = 0;
            var list = yy.UserList(name, page, limit, out count);
            var zhi = from n in list
                      select new UserDto()
                      {
                         UserID=n.userID,
                         UserName=n.userName,
                         Sex=n.sex,
                         Birthday=n.birthday,
                         Phone=n.phone,
                         Path=n.Images.path,
                         Status=n.status,
                         RoleName=n.Roles.roleName,
                         LoginName=n.loginName,
                         LoginPwd=n.loginPwd,
                         RoleID=n.Roles.roleID
                      };
            ResultPageList<UserDto> result = new ResultPageList<UserDto>();
            result.code = 0;
            result.msg = "ok";
            result.data = zhi.ToList();
            result.count = count;
            return result;
        }

        //会员查询
        [Route("api/vippage")]
        public ResultPageList<UserDto> Getvip(string name, int page, int limit)
        {

            int count = 0;
            var list = yy.VipList(name, page, limit, out count);
            var zhi = from n in list
                      select new UserDto()
                      {
                          UserID = n.userID,
                          UserName = n.userName,
                          Sex = n.sex,
                          Birthday = n.birthday,
                          Phone = n.phone,
                          Path = n.Images.path,
                          Status = n.status,
                          RoleName = n.Roles.roleName,
                          LoginName = n.loginName,
                          LoginPwd = n.loginPwd,
                          RoleID = n.Roles.roleID
                      };

            ResultPageList<UserDto> result = new ResultPageList<UserDto>();
            result.code = 0;
            result.msg = "ok";
            result.data = zhi.ToList();
            result.count = count;
            return result;
        }

        public ResultData<UserDto> Get(int id)
        {


            var list = yy.Query().Where(n => n.userID == id);
            //UserDto bb = new UserDto();

            var zhi = from n in list
                      select new UserDto()
                      {
                          Phone = n.phone,
                          UserName = n.userName,
                          Birthday = n.birthday,
                          Sex = n.sex,
                          RoleName=n.Roles.roleName
                      };
            ResultData <UserDto> result = new ResultData<UserDto>();
            result.code = 1;
            result.msg = "ok";
            result.data = zhi.FirstOrDefault();

            return result;
        }

        // POST: api/User
        [HttpPost]
        [Route("api/Add")]
        public Result Post([FromBody] UserAddDto user)
        {
           
            Users yh = new Users();
            yh.userName = user.UserName;
            yh.loginName =user.LoginName;
            yh.loginPwd =MD5Helper.Md5(user.LoginPwd);
            yh.sex = user.Sex;
            yh.birthday = user.Birthday;
            yh.phone = user.Phone;
            yh.imgID = user.ImgID;
            yh.status =user.Status;
            yh.lastTime =DateTime.Now;
            yh.roleID =user.RoleID;
            yy.Dal.Add(yh);
            bool count = yy.Dal.SaveChanges();
            Result result = new Result();
            result.code = count ? 1 : 0;
            result.msg = count ? "添加成功" : "添加失败";
            return result;


        }
        [HttpPut]
        [Route("api/User/Update1")]
        public Result PostUpdate([FromBody] UserAddDto user)
        {

            Users yh = yy.Dal.Query().Where(n => n.userID == user.UserID).FirstOrDefault();
            //Users yh = new Users();
            yh.userName = user.UserName;
            yh.loginName = user.LoginName;
            yh.sex = user.Sex;
            yh.birthday = user.Birthday;
            yh.phone = user.Phone;
            yh.status = user.Status;
            yh.lastTime = DateTime.Now;
            yh.roleID = user.RoleID;
            yy.Dal.Update(yh);
            bool count = yy.Dal.SaveChanges();
            Result result = new Result();
            result.code = count ? 1 : 0;
            result.msg = count ? "修改成功" : "修改失败";
            return result;
           

        }

        [HttpPut]
        [Route("api/User/gai")]
        public Result PostUpdate1([FromBody] UserAddDto user)
        {
            Users yh = yy.Dal.Query().Where(n => n.userID == user.UserID).FirstOrDefault();
            yh.userName = user.UserName;
            yh.sex = user.Sex;
            yh.birthday = user.Birthday;
            yh.phone = user.Phone;                        
                     
            yy.Dal.Update(yh);
            bool count = yy.Dal.SaveChanges();
            Result result = new Result();
            result.code = count ? 1 : 0;
            result.msg = count ? "修改成功" : "修改失败";
            return result;
        }

        [HttpPut]
        [Route("api/User/gai1")]
        public Result PostUpdate2([FromBody] UserAddDto user)
        {
            Users yh = yy.Dal.Query().Where(n => n.userID == user.UserID&&n.loginName==user.LoginName).FirstOrDefault();
            
            yh.loginPwd = MD5Helper.Md5(user.LoginPwd, "X2");
            yy.Dal.Update(yh);
            bool count = yy.Dal.SaveChanges();
            Result result = new Result();
            result.code = count ? 1 : 0;
            result.msg = count ? "修改成功" : "修改失败";
            return result;
        }

        // DELETE: api/User/5
        [HttpDelete]
        public Result Delete(int id)
        {
            bool count = yy.Delete(id);

            Result result = new Result();
            result.code = count ? 1 : 0;
            result.msg = count ? "删除成功" : "删除失败";
            return result;
        }
       
        [Route("api/User/Dels")]
        public Result Deletes([FromBody] int[] ids)
        {
            bool count =yy.Deletes(ids);
            Result result = new Result();
            result.code = count? 1 : 0;
            result.msg = count ? "删除成功" : "删除失败";
            return result;
        }
    }
}
